﻿using FairyGUI;
using System;
using System.Collections.Generic;
using System.Linq;

public class HeroInfoPanel : BasePopupPanel
{
    private Hero hero;

    private GButton btnChangeHero;
    private GTextField tfTitle;
    private GTextField tfInfo;
    private GTextField tfHp;
    private GTextField tfAtk;
    private GTextField tfDef;
    private GTextField tfRgs;
    private GTextField tfDex;
    private GTextField tfCrt;
    private GTextField tfEvd;
    private GTextField tfKbl;
    private GList lsJinJieUp;
    private GTextField tfSkill1;
    private GTextField tfSkillInfo1;
    private GTextField tfSkill2;
    private GTextField tfSkillInfo2;

    public override void Init()
    {
        base.Init();

        frame = view.GetChild("frame").asCom;
        closeTransition = view.GetTransition("t1");

        btnChangeHero = frame.GetChild("n43").asButton;
        tfTitle = frame.GetChild("title").asTextField;
        tfInfo = frame.GetChild("n4").asTextField;
        tfHp = frame.GetChild("n6").asTextField;
        tfAtk = frame.GetChild("n15").asTextField;
        tfDef = frame.GetChild("n18").asTextField;
        tfRgs = frame.GetChild("n21").asTextField;
        tfDex = frame.GetChild("n24").asTextField;
        tfCrt = frame.GetChild("n27").asTextField;
        tfEvd = frame.GetChild("n30").asTextField;
        tfKbl = frame.GetChild("n33").asTextField;
        lsJinJieUp = frame.GetChild("n35").asList;
        tfSkill1 = frame.GetChild("n37").asTextField;
        tfSkillInfo1 = frame.GetChild("n38").asTextField;
        tfSkill2 = frame.GetChild("n40").asTextField;
        tfSkillInfo2 = frame.GetChild("n41").asTextField;

        btnChangeHero.onClick.Add(OnChangeClick);

        List<RoleUp> upList = RoleUpMgr.Dict.Values.ToList();
        for (int i=0; i< upList.Count; i++)
        {
            GTextField gText = UIPackage.CreateObject("Main", "text_jinjieadd").asCom.GetChild("n0").asTextField;
            gText.text = upList[i].ToShowInfo();
            gText.color = CommonUtil.White;
            gText.data = upList[i].level;
            lsJinJieUp.AddChild(gText);
        }
    }

    public override void Show(params object[] para)
    {
        MsgListenerMgr.AddMsgListener(MsgListenerType.ShangZhen, OnShangZhenMsg);
        this.hero = para[0] as Hero;

        btnChangeHero.visible = hero.szPos > 1;

        this.SetInfo();

        base.Show(para);
    }

    public override void Close()
    {
        MsgListenerMgr.RemoveMsgListener(MsgListenerType.ShangZhen, OnShangZhenMsg);
        hero = null;
        base.Close();
    }

    protected override GComponent CreateView()
    {
        return UIPackage.CreateObject("Main", "panel_heroInfo1").asCom;
    }

    private void SetInfo()
    {
        if (hero == null)
        {
            return;
        }

        InitHero initRole = hero.InitHero;
        if (initRole == null)
        {
            return;
        }

        string name = hero.IsPlayer() ? GameNetwork.Player.Name : initRole.name;
        tfTitle.text = string.Format("{0}({1})(+{2})({3}星)", name,CommonUtil.GetDuJieName(hero.DuJieLevel),hero.JinJieLevel, hero.StarLevel);
        tfTitle.color = CommonUtil.GetHeroPinZhiColor(initRole.quality);
        tfInfo.text = initRole.info;

        Attribute attribute = hero.szPos == 0 ? hero.Attribute : GameNetwork.Player.FighAttributeMgr.Get(hero.szPos);

        tfHp.text = CommonUtil.NumberToString(attribute.Get(AttributeType.HP));
        tfAtk.text = CommonUtil.NumberToString(attribute.Get(AttributeType.ATK));
        tfDef.text = CommonUtil.NumberToString(attribute.Get(AttributeType.DEF));
        tfRgs.text = CommonUtil.NumberToString(attribute.Get(AttributeType.RGS));
        tfDex.text = string.Format("{0}%", CommonUtil.GetRate(attribute.Get(AttributeType.DEX))); 
        tfEvd.text = string.Format("{0}%", CommonUtil.GetRate(attribute.Get(AttributeType.EVD)));
        tfCrt.text = string.Format("{0}%", CommonUtil.GetRate(attribute.Get(AttributeType.CRT)));
        tfKbl.text = string.Format("{0}%", CommonUtil.GetRate(attribute.Get(AttributeType.KBL)));

        GObject[] gObjects = lsJinJieUp.GetChildren();
        int idx = -1;
        for (int i=0; i<gObjects.Length; i++)
        {
            GTextField gText = gObjects[i].asTextField;
            if (gText == null)
            {
                continue;
            }

            int value = (int)gText.data;
            if (value <= hero.DuJieLevel)
            {
                gText.color = CommonUtil.Green;
                idx = i;
            }
            else
            {
                gText.color = CommonUtil.White;
            }
        }
        if (idx != -1)
        {
            lsJinJieUp.ScrollToView(idx);
        }

        if (InitSkillMgr.Dict.TryGetValue(initRole.normal_skill_id, out InitSkill initSkill1))
        {
            tfSkill1.text = initSkill1.name;
            tfSkillInfo1.text = initSkill1.info;
        }

        if (InitSkillMgr.Dict.TryGetValue(initRole.proper_skill_id, out InitSkill initSkill2))
        {
            tfSkill2.text = initSkill2.name;
            tfSkillInfo2.text = initSkill2.info;
        }
    }

    private void OnChangeClick(EventContext context)
    {
        CommonUtil.ChangeHero(hero.szPos);
    }

    private void OnShangZhenMsg(object[] para)
    {
        Close();
    }
}
